package com.d.ydfactory.impl;

import com.alibaba.fastjson.JSONObject;
import com.d.business.domain.dto.BAnnexDTO;
import com.d.client.YidaClient;
import com.d.client.dto.YidaFromInstanceResultDTO;
import com.d.client.dto.YidaFromInstanceWriteDTO;
import com.d.ydfactory.constants.ApproveConstant;
import com.d.ydfactory.constants.PaymentConstanct;
import com.d.common.utils.YidaFileAddressToJson;
import com.d.ydfactory.YdApproveFactory;
import com.d.ydfactory.domain.ApproveStartResult;
import com.d.business.domain.dto.BPaymentDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;

@Service
public class PaymentApproveImpl extends BaseImpl implements YdApproveFactory {

    private static final Logger log = LoggerFactory.getLogger(PaymentApproveImpl.class);

    @Autowired
    private YidaClient yidaClient;

    @Override
    public ApproveStartResult start(Integer bussId) {
        ApproveStartResult result = new ApproveStartResult();
        try {
            //需实现获取业务数据接口
            BPaymentDTO bPaymentDTO =new BPaymentDTO();
//            BPaymentDTO bPaymentDTO = bPaymentConsumer.getById(bussId);


            YidaFromInstanceWriteDTO dto = new YidaFromInstanceWriteDTO();
            dto.setAppType(ApproveConstant.YIDA_APP_CODE);
            dto.setSystemToken(ApproveConstant.YIDA_APP_SECRET);
            dto.setFormUuid(ApproveConstant.paymentApproveItem.get("FROM_UUID"));
            dto.setProcessCode(ApproveConstant.paymentApproveItem.get("PROCESS_CODE"));
            dto.setUserId(bPaymentDTO.getRUserId());
            JSONObject json = new JSONObject();
            BigDecimal payPrice = BigDecimal.ZERO;
            if (bPaymentDTO.getPayPrice() != null) {
                payPrice = new BigDecimal(bPaymentDTO.getPayPrice()).divide(new BigDecimal(100));
            }
            json.put(ApproveConstant.paymentApproveItem.get("付款单号"), bPaymentDTO.getSerialNo());
            String deptId = bPaymentDTO.getRDtId().split(",")[0];
            for (String d : bPaymentDTO.getRDtId().split(",")) {
                if (ApproveConstant.dingProcessDept.get(d) != null) {
                    deptId = d;
                    break;
                }
            }
            json.put(ApproveConstant.paymentApproveItem.get("申请部门"),new String[]{deptId});
            json.put(ApproveConstant.paymentApproveItem.get("付款金额"), payPrice);
            json.put(ApproveConstant.paymentApproveItem.get("付款方式"), PaymentConstanct.paymentMode.get(bPaymentDTO.getPaymentMode()));
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");

//            json.put(ApproveConstant.paymentApproveItem.get("支付时间"), simpleDateFormat.format(bPaymentDTO.getPayTime()));
            json.put(ApproveConstant.paymentApproveItem.get("收款公司"), bPaymentDTO.getPayee());
            json.put(ApproveConstant.paymentApproveItem.get("支行名称"), bPaymentDTO.getBankBranch());
            json.put(ApproveConstant.paymentApproveItem.get("备注"), bPaymentDTO.getRemark());
            json.put(ApproveConstant.paymentApproveItem.get("银行账户"), bPaymentDTO.getBankAccount());
            json.put(ApproveConstant.paymentApproveItem.get("付款公司"),bPaymentDTO.getPurName());
            json.put(ApproveConstant.paymentApproveItem.get("来源单号"), bPaymentDTO.getOriginNo());
            json.put(ApproveConstant.paymentApproveItem.get("合作模式"), bPaymentDTO.getCoModel());
            json.put(ApproveConstant.paymentApproveItem.get("附件"), YidaFileAddressToJson.getFileStr(new ArrayList<BAnnexDTO>(),2));
            json.put(ApproveConstant.paymentApproveItem.get("图片"), YidaFileAddressToJson.getFileStr(new ArrayList<BAnnexDTO>(),1));


            dto.setFormDataJson(json.toString());
            YidaFromInstanceResultDTO resultDTO = yidaClient.createProcess(dto);

            result.setInstanceId(resultDTO.getResult());
            result.setCode("0");
            result.setMsg("成功");
            result.setBusinessNo(bPaymentDTO.getSerialNo());
            result.setFromUuid(dto.getFormUuid());
            result.setStaffId(dto.getUserId());

            return result;
        } catch (Exception e) {
            log.error(e.toString());
            result.setCode("-1");
            result.setMsg(e.getMessage());
            return result;
//            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateStatus(Integer processType, String businessNo, String instanceId, Integer status, String remark, String staffId) {
        //update status
        return 1;
    }
}
